/*
 * @author yjiewei
 * @date 2022/3/21 20:25
 */
package com.wu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wu.yygh.hosp.repository.DepartmentRepository;
import com.wu.yygh.hosp.service.DepartmentService;
import com.wu.yygh.model.hosp.Department;
import com.wu.yygh.vo.hosp.DepartmentQueryVo;
import com.wu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> map) {
        // 首先将json数据转换成字符串，再转换成对象
        // 本质上map就是一个json数据
        // 这俩操作很重要
        String departmentString = JSONObject.toJSONString(map);
        Department department = JSONObject.parseObject(departmentString, Department.class);

        // 判断是否存在数据
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        // 不存在则添加
        if (Objects.isNull(departmentExist)) {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else {
            // 否则修改
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 1.创建Pageable对象，设置当前页和每页记录数 0是第一页
        PageRequest pageAble = PageRequest.of(page - 1, limit);

        // 2.增加查询条件
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);

        // 3.构造匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);

        // 4.查分页数据
        return departmentRepository.findAll(example, pageAble);

    }

    @Override
    public void removeDepartment(String hoscode, String depcode) {
        //根据医院编号 和 科室编号查询
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            //调用方法删除
            departmentRepository.deleteById(department.getId());
        }
    }

    /**
     * 根据医院编号
     * 查询医院所有科室列表
     * 将查出来的相同bigCode数据整合，封装一个父级结构
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> result = new ArrayList<>();

        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> of = Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(of);

        // 要封装一个树形结构，bigCode分组，获取每个大科室里面下级子科室
        // bigCode:subDepartment
        Map<String, List<Department>> collect =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));

        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            // 大科室以及对应的全局数据
            String bigCode = entry.getKey();
            List<Department> list = entry.getValue();

            // 封装大科室 多抽取了这一层
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(list.get(0).getBigname());

            // 封装小科室 只有两层吗
            List<DepartmentVo> children = new ArrayList<>();
            for (Department dept : list) {
                DepartmentVo vo = new DepartmentVo();
                vo.setDepcode(dept.getDepcode());
                vo.setDepname(dept.getDepname());
                children.add(vo);
            }
            departmentVo.setChildren(children);
            result.add(departmentVo);
        }
        return result;
    }

    /**
     * 根据科室编号，医院编号，查询科室名称
     */
    @Override
    public String getDepName(String hoscode, String depcode) {

        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (Objects.nonNull(department)) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
